//@Lim Wei Ke A0083402R
package logic;

import global.Task;

import java.util.ArrayList;

import storage.Storage;

public class UndoCommand extends Command {
	
	private boolean isExecutable;
	private String feedback;
	
	public UndoCommand(boolean isExecutable, String feedback) {
		this.isExecutable = isExecutable;
		this.feedback = feedback;
	}
	
	public ArrayList<String> execute(){
		ArrayList<String> newFeedback = new ArrayList<String>();
		
		if (isExecutable){
			DisplayCommand lastDisplayed;
			try{				
				if (!ExecutionHandler.getIsUndo()){	
					switch (ExecutionHandler.getMostRecentCommandType()) {
					case ADD:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.deleteFile(ExecutionHandler.getOldTasks().get(i).getTaskFileName());				
						feedback = "\"Add\" command undo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						ExecutionHandler.setIsUndo(true);
						break;
						
					case DELETE:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getOldTasks().get(i));
						feedback = "\"Delete\" command undo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						ExecutionHandler.setIsUndo(true);
						break;
						
					case UPDATE: 
						Task updatedTask = ExecutionHandler.getUpdatedTasks().get(0);
						Task oldTask = ExecutionHandler.getOldTasks().get(0);
						Storage.deleteFile(oldTask.getTaskFileName());
						Storage.addFile(updatedTask);
						feedback = "\"Update\" command undo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						ExecutionHandler.setIsUndo(true);
						break;
						
					case MARKDONE:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.deleteFile(ExecutionHandler.getOldTasks().get(i).getTaskFileName());
						for (int i = 0; i < ExecutionHandler.getUpdatedTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getUpdatedTasks().get(i));
						feedback = "\"Markdone\" command undo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						ExecutionHandler.setIsUndo(true);
						break;
						
					case MARKUNDONE:
						for (int i = 0; i < ExecutionHandler.getOldTasks().size(); i++)
							Storage.deleteFile(ExecutionHandler.getOldTasks().get(i).getTaskFileName());
						for (int i = 0; i < ExecutionHandler.getUpdatedTasks().size(); i++)
							Storage.addFile(ExecutionHandler.getUpdatedTasks().get(i));
						feedback = "\"MarkUndone\" command undo.";
						lastDisplayed = ExecutionHandler.getSecondRecentDisplay();
						newFeedback = lastDisplayed.display();
						ExecutionHandler.setIsUndo(true);
						break;
						
					case DISPLAY:
						feedback = "Cannot undo a \"display\" Command.";
						lastDisplayed = ExecutionHandler.getRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case OPEN:
						feedback = "Cannot undo a \"Calendar\" Command.";
						lastDisplayed = ExecutionHandler.getRecentDisplay();
						newFeedback = lastDisplayed.display();		
						break;
						
					case REDO:
						feedback = "Cannot undo a \"Redo\" Command.";
						lastDisplayed = ExecutionHandler.getRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					case CONNECT:
						feedback = "Cannot undo a \"Connect\" Command.";
						lastDisplayed = ExecutionHandler.getRecentDisplay();
						newFeedback = lastDisplayed.display();
						break;
						
					default: 
						feedback = "Unknown error.";
					}			
				}	
				else {
					feedback = "Cannot undo multiple times.";
					lastDisplayed = ExecutionHandler.getRecentDisplay();
					newFeedback = lastDisplayed.display();
					ExecutionHandler.setIsUndo(false);

				}
			} catch(Exception e){		
				lastDisplayed = ExecutionHandler.getRecentDisplay();
				newFeedback = lastDisplayed.display();
				feedback = ("Undo Command " + ExecutionHandler.getMostRecentCommandType() + " unsuccessful");
			}
		}
		newFeedback.add(feedback);
		return newFeedback;
	}
	
	public String getTestFeedback() {
		return "undo";
	}
}
//@Lim Wei Ke A0083402R